package cn.nfwx.tv.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.helper.RecyclerViewHelper;
import cn.nfwx.tv.helper.ThreadHelper;
import cn.nfwx.tv.model.QpopProgram;
import cn.nfwx.tv.util.LogUtil;
import cn.nfwx.tv.util.RecyclerViewUtil;
import cn.nfwx.tv.util.TimeUtil;
import cn.nfwx.tv.widget.qpop.FadeRecyclewView;
import cn.nfwx.tv.widget.qpop.QpopDateAdapter;
import cn.nfwx.tv.widget.qpop.QpopProgramAdapter;

/**
 * Created by dell on 2018/4/4.
 */

public class QpopReviewView extends LinearLayout {

    private final LoadView loadView;
    private final FrameLayout fl_content;
    private boolean _isclearDate = false;
    private boolean _isLive = true;
    private boolean _isFirstInit = true;

    private FadeRecyclewView rcv_qpop_review_date;
    private RecyclerViewHelper recyclerViewHelper;
    private RecyclerView rcv_qpop_review_program;
    private QpopDateAdapter qpopDateAdapter;
    private QpopProgramAdapter qpopProgramAdapter;
    private QpopReviewItemClickListener itemClickListener;

    private int playingPostion = -1;

    public void setItemClickListener(QpopReviewItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface QpopReviewItemClickListener {
        void dateItemClick(String dateStr);

        void programItemClick(QpopProgram program);
    }

    public QpopReviewView(Context context) {
        this(context, null);
    }

    public QpopReviewView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QpopReviewView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_qpop_review, this);

        loadView = new LoadView(context);

        rcv_qpop_review_date = findViewById(R.id.rcv_qpop_review_date);
        recyclerViewHelper = new RecyclerViewHelper().attachToRecyclerView(rcv_qpop_review_date);

        rcv_qpop_review_program = findViewById(R.id.rcv_qpop_review_program);

        fl_content = findViewById(R.id.fl_content);

        RecyclerViewUtil.setLinearLayoutManagerHorizontalWithoutDivder(context, rcv_qpop_review_date);
        rcv_qpop_review_date.addItemDecoration(new VerticalDividerItemDecoration.Builder(context).size((int) getResources().getDimension(R.dimen.dp20)).color(Color.TRANSPARENT).build());

        RecyclerViewUtil.setLinearLayoutManagerWithoutDivder(context, rcv_qpop_review_program);
        //rcv_qpop_review_program.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context).size((int) getResources().getDimension(R.dimen.dp35)).color(Color.TRANSPARENT).build());

        qpopDateAdapter = new QpopDateAdapter();
        rcv_qpop_review_date.setAdapter(qpopDateAdapter);

        qpopProgramAdapter = new QpopProgramAdapter();
        rcv_qpop_review_program.setAdapter(qpopProgramAdapter);

        qpopDateAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (itemClickListener != null) {
                    qpopDateAdapter.setSelection(position);
                    qpopProgramAdapter.isPlayDay(qpopDateAdapter.getItem(position)); // 日期是否
                    recyclerViewHelper.smoothScrollToPositionCenter(position);
                    clearPrograms();
                    itemClickListener.dateItemClick(TimeUtil.getTodayYYMMdd(position - 3));
                }
            }
        });

        qpopProgramAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (itemClickListener != null) {
                    QpopProgram program = qpopProgramAdapter.getItem(position);
                    if (program != null && !TextUtils.isEmpty(program.getVideoUrl())) {
                        _isLive = false;
                        qpopProgramAdapter.setDateStr(qpopDateAdapter.getItem(qpopDateAdapter.getSelection()));
                        qpopProgramAdapter.isPlayDay(true);
                        qpopProgramAdapter.setSelection(position);
                        qpopProgramAdapter.notifyDataSetChanged();
                        itemClickListener.programItemClick(program);
                    }
                }
            }
        });
    }

    /**
     * 设置时间
     *
     * @param ds
     */
    public void setNewDates(List<String> ds) {
        List<String> dates = new ArrayList<>();
        dates.addAll(ds);
        qpopDateAdapter.setNewData(dates);
    }

    /**
     * 设置节目列表
     */
    public void setNewPrograms(List<QpopProgram> datas) {
        if (datas.isEmpty()) {
            loadView.loadNodata();
        } else {
            loadView.stopLoading(fl_content);
        }
        List<QpopProgram> programs = new ArrayList<>();
        programs.addAll(datas);
        playingPostion = -1;
        for (int i = 0; i < programs.size(); i++) {
            QpopProgram program = programs.get(i);
            if (program != null && program.getIsPlaying() == 0) {
                playingPostion = i;
                break;
            }
        }
        // 判断节目列表是否显示中，显示自动滑动
        if (rcv_qpop_review_program != null && rcv_qpop_review_program.getMeasuredWidth() != 0) {
            if (playingPostion != -1) {
                ((LinearLayoutManager) rcv_qpop_review_program.getLayoutManager()).scrollToPositionWithOffset(playingPostion, (int) (rcv_qpop_review_program.getHeight() / 2 - getResources().getDimension(R.dimen.dp40) / 2));
            } else {
                rcv_qpop_review_program.smoothScrollToPosition(0);
            }
        }
        qpopProgramAdapter.setNewData(programs);
    }

    public void loadError() {
        loadView.loadNodata();
    }

    /**
     * 清空节目表
     */
    public void clearPrograms() {
        loadView.startLoading(fl_content);
        qpopProgramAdapter.setNewData(new ArrayList<QpopProgram>());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        _isFirstInit = true;
    }

    @Override
    protected void onAnimationEnd() {
        super.onAnimationEnd();
        if (_isclearDate) {
            _isclearDate = false;
            qpopDateAdapter.setSelection(3);
            qpopProgramAdapter.setDateStr(qpopDateAdapter.getItem(qpopDateAdapter.getSelection()));
            qpopProgramAdapter.isPlayDay(true);
            qpopProgramAdapter.setSelection(playingPostion);
            rcv_qpop_review_date.scrollToPosition(3);
            if (_isFirstInit) {
                rcv_qpop_review_program.scrollToPosition(playingPostion);
            }
            // ((LinearLayoutManager) rcv_qpop_review_program.getLayoutManager()).scrollToPositionWithOffset(playingPostion, (int) (rcv_qpop_review_program.getHeight() / 2 - getResources().getDimension(R.dimen.dp53) / 2));
            ThreadHelper.postDelayed(new Runnable() {
                @Override
                public void run() {
                    recyclerViewHelper.smoothScrollToPositionCenter(3);
                    if (_isFirstInit) {
                        _isFirstInit = false;
                        rcv_qpop_review_program.scrollBy(0, (int) (rcv_qpop_review_program.getHeight() / 2 - getResources().getDimension(R.dimen.dp53) / 2));
                    }
                }
            }, 100);
        }
    }

    /**
     * 清除日期数据
     */
    public void clearDate() {
        _isLive = true;
        _isclearDate = true;
    }
}
